import 'package:kkhousekeep/models/me_user_info_entity.dart';
import 'package:kkhousekeep/net/base_connect.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../models/base_respond_entity.dart';
import '../models/image_upload_entity.dart';
import 'api.dart';

class MeAccountConnect extends BaseConnect {
  Future<ImageUploadEntity?> modifyAvatarForProperty({
    required String imageUrl,
  }) async {
    var response = await patch(Api.modifyAvatarForProperty, {
      'headPortrait': imageUrl,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    var entity = ImageUploadEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<BaseRespondEntity?> modifyPasswordForProperty({
    required String oldPassword,
    required String newPassword,
  }) async {
    var response = await patch(Api.modifyPasswordForProperty, {
      'oldPassword': oldPassword,
      'newPassword': newPassword,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    BaseRespondEntity entity = BaseRespondEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<ImageUploadEntity?> updateUserInfo({
    String? userName,
    String? userImage,
  }) async {
    var response = await post(Api.updateUserInfo, {
      'userName': userName,
      'userImage': userImage,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    var entity = ImageUploadEntity.fromJson(response.body);
    return Future.value(entity);
  }

  Future<MeUserInfoObj?> getUserInfo({
    String? userName,
    String? userImage,
  }) async {
    var response = await post(Api.getUserInfo, {
      'userName': userName,
      'userImage': userImage,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(MeUserInfoEntity.fromJson(response.body).obj);
  }
}
